package com.sgu6.sgu6_machine.ui.fragment.set;

import android.app.AlertDialog;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgu6.middleware.util.HexUtil;
import com.sgu6.middleware.enums.MachineCmdEnum;
import com.sgu6.serialport.manager.SerialPortManager;
import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.base.BaseF;
import com.sgu6.sgu6_machine.config.MachineConfig;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.ui.views.AmountView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 警报设置
 */
public class WeightF extends BaseF {

    private AlertDialog.Builder builder;
    @BindView(R.id.atWeightCheck)
    AmountView atWeightCheck;
    @BindView(R.id.atOffsetWeight)
    AmountView atOffsetWeight;

    public static WeightF newInstance() {
        return new WeightF();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_weight, container, false);
        unbinder = ButterKnife.bind(this, v);

        initUI();
        initDialog();
        return v;
    }

    public void initUI() {
        atOffsetWeight.setDefaultValue(MachineContainer.machineConfigDto.offsetWeight);
        atOffsetWeight.setAmountMaxValue(50);
        atWeightCheck.setDefaultValue(MachineContainer.machineConfigDto.adjustWeight);
    }

    private void initDialog() {
        builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("提示");
        builder.setMessage("是否进行重量标定?");
        builder.setNegativeButton("取消", (dialog, which) -> {
        });

        builder.setPositiveButton("确定", (dialog, which) -> {
            String cw = String.format(MachineCmdEnum.重量标定.code(), atWeightCheck.getValue());
            SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(cw));
            MachineContainer.machineConfigDto.adjustWeight = (atWeightCheck.getValue());
            updateConfig();
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.btnSetWeightCompensate, R.id.btnSetWeight})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btnSetWeightCompensate: //设置重量补偿
                String wcc = String.format(MachineCmdEnum.设置重量补偿值.code(), atOffsetWeight.getValue());
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(wcc));
                MachineContainer.machineConfigDto.offsetWeight = (atOffsetWeight.getValue());
                updateConfig();
                break;
            case R.id.btnSetWeight: //重量标定
                builder.show();
                break;
        }
    }

}
